package cn.fage.servlet;

import com.alibaba.fastjson.JSON;

import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/MyServlet")
public class MyServlet implements Servlet {

    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("初始化servlet");
    }

    @Override
    public ServletConfig getServletConfig() {
        System.out.println("getServletConfig()");
        return null;
    }

    @Override
    public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
        System.out.println("正在销毁");
        PrintWriter writer = servletResponse.getWriter();
        Map<String, Object> map = new HashMap<>();
        map.put("key01", "hello world ");
        map.put("key02", true);
//        System.out.println(1/0);
        writer.println(JSON.toJSONString(map));
//        writer.print(JSON.parse(JSON.toJSONString(map)));
    }

    @Override
    public String getServletInfo() {
        System.out.println("getServletInfo()");
        return null;
    }

    @Override
    public void destroy() {
        System.out.println("正在销毁");
    }
}